/*
 * MainApplet.java
 *
 * Created on 12.02.2011, 15:03:12
 */

package viewer;

import java.awt.Color;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Иван
 */
public class MainApplet extends javax.swing.JApplet implements ColorChoseable {
    public static String selectedDoor ="door1";
    public static final int[] plColors = {0xeceadd, 0x724f30, 0xb46933, 0x572a19,
        0xbb8353, 0x95582d, 0x76401d, 0x452a19, 0x815533, 0xb27f57, 0xcbbfb0,
        0x1d191a, 0xcb985e};

    MyColorPanel mcp;

    public static void main(String[] args) {
        JFrame jf = new JFrame("Divasoft CatalogViewer.MainApplet");
        jf.setSize(750, 700);
        jf.setLocation(100, 0);
        MainApplet ma = new MainApplet();
        ma.init();
        jf.getContentPane().add(ma);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
    }

    public void onCreate() {
        mcp = new MyColorPanel(this);
        mcp.setLocation(12, 45);
        getContentPane().add(mcp);
        //Locale.setDefault(new Locale("ru","RU"));
        //JColorChooser.setDefaultLocale(new Locale("ru","RU"));
        for (int i=0; i<plColors.length; i++)
            jPanel2.add(CreatePanelWithParams(new Color(plColors[i])));

        jPanel3.setBackground(new Color(0xccc3ab));
        jPanel1.setBackground(new Color(0xccc3ab));
        jPanel4.setBackground(new Color(plColors[plColors.length-2]));

        jButton1.setLocation((jPanel3.getWidth()-jButton1.getWidth())/2,
                jPanel3.getHeight()-jButton1.getHeight());

        Image image = getImage(getDocumentBase(), "./catalog/beloe-derevo.jpg");
        ImageIcon ic = new ImageIcon(image);
        jButton1.setIcon(ic);
    }

    private void colorPlintusSel(Color c) {
        jPanel4.setBackground(c);
        repaint();
    }

    public JPanel CreatePanelWithParams(Color backCol) {
        JPanel j1Panel1 = new JPanel();
        j1Panel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        j1Panel1.setMaximumSize(new java.awt.Dimension(25, 25));
        j1Panel1.setMinimumSize(new java.awt.Dimension(25, 25));
        j1Panel1.setPreferredSize(new java.awt.Dimension(25, 25));
        j1Panel1.setBackground(backCol);
        j1Panel1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        j1Panel1.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                colorPlintusSel(((JPanel)e.getComponent()).getBackground());
            }

            public void mousePressed(MouseEvent e) {

            }

            public void mouseReleased(MouseEvent e) {

            }

            public void mouseEntered(MouseEvent e) {

            }

            public void mouseExited(MouseEvent e) {

            }
        });
        return j1Panel1;
    }

    /** Initializes the applet MainApplet */
    @Override
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable(){
                public void run() {
                    initComponents();
                    onCreate();
                }
            });
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jPanel1.setMaximumSize(new java.awt.Dimension(25, 25));
        jPanel1.setMinimumSize(new java.awt.Dimension(25, 25));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 21, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 21, Short.MAX_VALUE)
        );

        jLabel1.setText("Цвет плинтуса");

        jPanel2.setMinimumSize(new java.awt.Dimension(175, 50));
        jPanel2.setPreferredSize(new java.awt.Dimension(175, 50));
        jPanel2.setLayout(new java.awt.GridLayout(2, 7, 3, 3));

        jPanel3.setPreferredSize(new java.awt.Dimension(390, 445));
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton1.setBorder(null);
        jButton1.setBorderPainted(false);
        jButton1.setContentAreaFilled(false);
        jButton1.setFocusPainted(false);
        jButton1.setMaximumSize(new java.awt.Dimension(390, 445));
        jButton1.setMinimumSize(new java.awt.Dimension(390, 445));
        jButton1.setPreferredSize(new java.awt.Dimension(390, 445));
        jPanel3.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 80, 234, 390));

        jPanel4.setPreferredSize(new java.awt.Dimension(390, 13));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 390, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 13, Short.MAX_VALUE)
        );

        jPanel3.add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 460, 390, -1));

        jLabel2.setText("Цвет стены");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(80, 80, 80))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(127, 127, 127)
                                .addComponent(jLabel1))
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 470, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    // End of variables declaration//GEN-END:variables

    public void onChangeColor(Color color) {
        jPanel1.setBackground(color);
        jPanel3.setBackground(color);
    }

}
